var Q = require('q');
var AV = require('leanengine');
var async = require('async');

var signfunc = require('signfunc');

//~~~~~~~~~ 变量区 ~~~~~~~~~~~~

//var _allSignDay = undefined, _continualSignDay = undefined; //累计签到和连续签到天数



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~

module.exports = {
    //获取用户的签到信息

    //获取用户之前漏签的天数
    //返回值 { all: 0, delta: 0 } 签到累计天数,漏签天数
    getUserLostSignDayNumber:function(UID){
        var q = Q.defer();
        //并行执行，查找两个数据
        async.parallel([
            function(cb){
                //获取 AllSignDays
                var _query = new AV.Query('ActivitySignDayUser');
                _query.equalTo('Uid',UID);
                _query.first().then(function(signDayUser){
                    if(signDayUser == null){
                        //用户没有任何签到记录
                        cb(null,0);
                    }else{
                        cb(null,parseInt(signDayUser.get('AllSignDays')));
                    }
                })
            },

            function(cb){
                //获取 ContinualSignDays
                var _query = new AV.Query('ActivitySignDayDetail');
                _query.equalTo('Uid',UID);
                var signMonth = {} ;
                _query.find().then(function(data){
                    if(data){
                        async.eachSeries(data, function(each,eachCb){
                            signMonth[each.get('Year')] = each.toJSON() ;
                            eachCb(null, signMonth);
                        },function(){
                            //计算连续的天数
                            var now = new Date() - 1000 * 24 * 60 * 60;
                            now = new Date(now);
                            var days = signfunc.getDays(signMonth,{year:parseInt(now.getFullYear()),month:parseInt(now.getMonth()+1),day:parseInt(now.getDate())});
                            cb(null,parseInt(days));
                        })
                    }else{
                        //没有找到该用户的连续签到记录
                        cb(null,0);
                    }
                });
            }],
            function (err, results) {
                if(err){
                    //执行错误
                    //输出错误
                    console.log(err);
                    q.reject(err);
                }else{
                    //
                    q.resolve({all:results[0],delta:results[0] - results[1]});
                }
            }
        );
        return q.promise;
    },

    //获取用户100天内累计的消费金额
    getUserOrderAmountTheLast100Days:function(UID){
        var q = Q.defer();
        var _query = new AV.Query('Order');
        _query.equalTo('uid',UID);//下单用户
        _query.greaterThanOrEqualTo('status',2000);//订单已付款
        _query.greaterThanOrEqualTo('createdAt',new Date().getTime() - (100 * 1000 * 60 * 60 * 24));//从100天之前开始统计
        _query.find().then(function(list){
            async.map(list,
                function(ele,cb){
                    cb(null,parseFloat(ele.get('finalAmount')));
                },
                function(err,results){
                    if(err){

                    }else{
                        var sum = 0;
                        results.forEach(function(item){
                            sum += item;
                        });
                        q.resolve(sum);
                    }
                }
            )

        });
        return q.promise;
    },

    //获取用户近100天内的平均每周的下单量
    getUserOrdersTheLast100Days:function(UID){
        var q = Q.defer();
        var _query = new AV.Query('Order');
        _query.equalTo('uid',UID);//下单用户
        _query.greaterThanOrEqualTo('status',2000);//订单已付款
        _query.greaterThanOrEqualTo('createdAt',new Date().getTime() - (100 * 1000 * 60 * 60 * 24));//从100天之前开始统计
        _query.count().then(function(c){
            q.resolve(c);
        });
        return q.promise;
    },

    //获取用户的补签次数
    getUserFillSignDays:function(UID){
        var q = Q.defer();
        var _query = new AV.Query('PointFlow');
        _query.equalTo('userid',UID);//积分操作用户
        _query.contains('content',"补签扣除积分");//用户补签的记录
        _query.count().then(function(c){
            q.resolve(c);
        });
        return q.promise;
    },

    getUserSignDayGreateThen2:function(UID){
        var q = Q.defer();
        var _query = new AV.Query('PointFlow');
        _query.contains('content',"补签扣除积分");//用户补签的记录
        _query.count().then(function(c){
            q.resolve(c);
        });
        return q.promise;
    }

};